package com.hhl.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhl.flower.mapper.ShoppingCartMapper;
import com.hhl.flower.pojo.entity.Flower;
import com.hhl.flower.pojo.entity.ShoppingCart;
import com.hhl.flower.pojo.vo.ShoppingCartVO;
import com.hhl.flower.service.FlowerService;
import com.hhl.flower.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author Liubuzhu
* @description 针对表【shopping_cart】的数据库操作Service实现
* @createDate 2024-06-12 14:38:50
*/
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
    implements ShoppingCartService{

	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private FlowerService flowerService;

	public void addToCart(Integer userId, Integer flowerId, Integer number, Double amount) {
		// 首先要判断花花在购物车中是否已经存在，存在的花就在原有的基础上添加数量
		QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("u_id", userId);
		queryWrapper.eq("f_id", flowerId);
		ShoppingCart shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
		if (shoppingCart != null) {
			// 已经存在
			shoppingCart.setNumber(shoppingCart.getNumber() + number);
			shoppingCart.setAmount(shoppingCart.getAmount() + amount);
			shoppingCartMapper.updateById(shoppingCart);
		} else {
			// 不存在
			shoppingCart = new ShoppingCart();
			shoppingCart.setUId(userId);
			shoppingCart.setFId(flowerId);
			shoppingCart.setNumber(number);
			shoppingCart.setAmount(amount);
			shoppingCart.setCreateTime(LocalDateTime.now());
			shoppingCartMapper.insert(shoppingCart);
		}
	}

	public List<ShoppingCartVO> getCartByUserId(Integer userId) {
		QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("u_id", userId);
		List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
		List<ShoppingCartVO> shoppingCartVOS = new ArrayList<>();
		for (ShoppingCart shoppingCart : shoppingCarts) {
			Flower flower = flowerService.getById(shoppingCart.getFId());
			ShoppingCartVO shoppingCartVO = new ShoppingCartVO();
			shoppingCartVO.setFId(flower.getId());
			shoppingCartVO.setName(flower.getName());
			shoppingCartVO.setEname(flower.getEname());
			shoppingCartVO.setUnit(flower.getUnit());
			shoppingCartVO.setCoverUrl(flower.getCoverUrl());
			shoppingCartVO.setPrice(flower.getPrice());
			shoppingCartVO.setNumber(shoppingCart.getNumber());
			shoppingCartVO.setAmount(shoppingCart.getAmount());
			shoppingCartVO.setCreateTime(shoppingCart.getCreateTime());
			shoppingCartVO.setId(shoppingCart.getId());
			shoppingCartVO.setUId(shoppingCart.getUId());
			shoppingCartVOS.add(shoppingCartVO);
		}
		return shoppingCartVOS;
	}

	public void deleteFlowerFromCart(Integer fid) {
		QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("f_id", fid);
		shoppingCartMapper.delete(queryWrapper);
	}

	public void updateCartNumber(Integer fid, Integer number, Double amount) {
		QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("f_id", fid);
		ShoppingCart shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
		shoppingCart.setNumber(number);
		shoppingCart.setAmount(amount);

		shoppingCartMapper.updateById(shoppingCart);
	}

}




